# How Does Improvement in Commuting Affect Employees? Evidence from a Natural Experiment
# Version: 20211014

## Yao Lu (luyao@sem.tsinghua.edu.cn),        Tsinghua University
## Xinzheng Shi (shixzh@sem.tsinghua.edu.cn), Tsinghua University
## Jagadeesh Sivadasan (jagadees@umich.edu),  University of Michigan
## Zhufeng Xu (zhufeng@zhufengxu.com),        Central University of Finance and Economics

if (!exists("AllScriptFlag", 1)) {
  source("./Scripts/1Header.R")
}

#### Table 2: Impact of the Opening of Subway Line 15 on Employee Compensation
Main <- read_dta("Data/Main.dta")

summary(fit_logti_sns         <-  felm(LogTiT          ~ `Self-Reported NearSubway`:Post | ID + as.factor(Company):as.factor(Date) | 0 | ID + Date,  data = Main, exactDOF = TRUE))
summary(fit_logtinetbonus_sns <-  felm(LogTiNetBonus   ~ `Self-Reported NearSubway`:Post | ID + as.factor(Company):as.factor(Date) | 0 | ID + Date,  data = Main, exactDOF = TRUE))
summary(fit_bonus_sns         <-  felm(Bonus           ~ `Self-Reported NearSubway`:Post | ID + as.factor(Company):as.factor(Date) | 0 | ID + Date,  data = Main, exactDOF = TRUE))
summary(fit_logbonus_sns      <-  felm(LogBonusT       ~ `Self-Reported NearSubway`:Post | ID + as.factor(Company):as.factor(Date) | 0 | ID + Date,  data = Main, exactDOF = TRUE))

summary(fit_logti_ns          <-  felm(LogTiT          ~ NearSubway:Post | ID + as.factor(Company):as.factor(Date) | 0 | ID + Date,  data = Main, exactDOF = TRUE))
summary(fit_logtinetbonus_ns  <-  felm(LogTiNetBonus   ~ NearSubway:Post | ID + as.factor(Company):as.factor(Date) | 0 | ID + Date,  data = Main, exactDOF = TRUE))
summary(fit_bonus_ns          <-  felm(Bonus           ~ NearSubway:Post | ID + as.factor(Company):as.factor(Date) | 0 | ID + Date,  data = Main, exactDOF = TRUE))
summary(fit_logbonus_ns       <-  felm(LogBonusT       ~ NearSubway:Post | ID + as.factor(Company):as.factor(Date) | 0 | ID + Date,  data = Main, exactDOF = TRUE))

MainMatched <- read_dta("Data/MainMatched.dta")

summary(fit_logti_sns_matched         <-  felm(LogTiT          ~ `Self-Reported NearSubway`:Post | ID + as.factor(Company):as.factor(Date) | 0 | ID + Date,  data = MainMatched, subset = (!is.na(`Self-Reported NearSubway`)), exactDOF = TRUE))
summary(fit_logtinetbonus_sns_matched <-  felm(LogTiNetBonus   ~ `Self-Reported NearSubway`:Post | ID + as.factor(Company):as.factor(Date) | 0 | ID + Date,  data = MainMatched, subset = (!is.na(`Self-Reported NearSubway`)), exactDOF = TRUE))
summary(fit_bonus_sns_matched         <-  felm(Bonus           ~ `Self-Reported NearSubway`:Post | ID + as.factor(Company):as.factor(Date) | 0 | ID + Date,  data = MainMatched, subset = (!is.na(`Self-Reported NearSubway`)), exactDOF = TRUE))
summary(fit_logbonus_sns_matched      <-  felm(LogBonusT       ~ `Self-Reported NearSubway`:Post | ID + as.factor(Company):as.factor(Date) | 0 | ID + Date,  data = MainMatched, subset = (!is.na(`Self-Reported NearSubway`)), exactDOF = TRUE))

summary(fit_logti_ns_matched          <-  felm(LogTiT          ~ NearSubway:Post | ID + as.factor(Company):as.factor(Date) | 0 | ID + Date,  data = MainMatched, exactDOF = TRUE))
summary(fit_logtinetbonus_ns_matched  <-  felm(LogTiNetBonus   ~ NearSubway:Post | ID + as.factor(Company):as.factor(Date) | 0 | ID + Date,  data = MainMatched, exactDOF = TRUE))
summary(fit_bonus_ns_matched          <-  felm(Bonus           ~ NearSubway:Post | ID + as.factor(Company):as.factor(Date) | 0 | ID + Date,  data = MainMatched, exactDOF = TRUE))
summary(fit_logbonus_ns_matched       <-  felm(LogBonusT       ~ NearSubway:Post | ID + as.factor(Company):as.factor(Date) | 0 | ID + Date,  data = MainMatched, exactDOF = TRUE))


Table2_Plain <- bind_rows(
  GenRegTable_Enhanced(fit = fit_logti_sns,         depvar = "LogTiT",        type = "level", Data = filter(Main, !is.na(`Self-Reported NearSubway`))),
  GenRegTable_Enhanced(fit = fit_logtinetbonus_sns, depvar = "LogTiNetBonus", type = "level", Data = filter(Main, !is.na(`Self-Reported NearSubway`))),
  GenRegTable_Enhanced(fit = fit_bonus_sns,         depvar = "Bonus",         type = "level", Data = filter(Main, !is.na(`Self-Reported NearSubway`))),
  GenRegTable_Enhanced(fit = fit_logbonus_sns,      depvar = "LogBonusT",     type = "level", Data = filter(Main, !is.na(`Self-Reported NearSubway`))),
  
  GenRegTable_Enhanced(fit = fit_logti_ns,          depvar = "LogTiT",        type = "level"),
  GenRegTable_Enhanced(fit = fit_logtinetbonus_ns,  depvar = "LogTiNetBonus", type = "level"),
  GenRegTable_Enhanced(fit = fit_bonus_ns,          depvar = "Bonus",         type = "level"),
  GenRegTable_Enhanced(fit = fit_logbonus_ns,       depvar = "LogBonusT",     type = "level"),
  
  GenRegTable_Enhanced(fit = fit_logti_sns_matched,         depvar = "LogTiT",        type = "level", Data = filter(MainMatched, !is.na(`Self-Reported NearSubway`))),
  GenRegTable_Enhanced(fit = fit_logtinetbonus_sns_matched, depvar = "LogTiNetBonus", type = "level", Data = filter(MainMatched, !is.na(`Self-Reported NearSubway`))),
  GenRegTable_Enhanced(fit = fit_bonus_sns_matched,         depvar = "Bonus",         type = "level", Data = filter(MainMatched, !is.na(`Self-Reported NearSubway`))),
  GenRegTable_Enhanced(fit = fit_logbonus_sns_matched,      depvar = "LogBonusT",     type = "level", Data = filter(MainMatched, !is.na(`Self-Reported NearSubway`))),
  
  GenRegTable_Enhanced(fit = fit_logti_ns_matched,          depvar = "LogTiT",        type = "level", Data = MainMatched),
  GenRegTable_Enhanced(fit = fit_logtinetbonus_ns_matched,  depvar = "LogTiNetBonus", type = "level", Data = MainMatched),
  GenRegTable_Enhanced(fit = fit_bonus_ns_matched,          depvar = "Bonus",         type = "level", Data = MainMatched),
  GenRegTable_Enhanced(fit = fit_logbonus_ns_matched,       depvar = "LogBonusT",     type = "level", Data = MainMatched))


Label1_DepVar <- c("", "Log(Total ", "", "")
Label2_DepVar <- c("Log (Total", "Income Net ", "Bonus", "Log ")
Label3_DepVar <- c("Income$^\\text{T}$)", "of Bonus)", "", "(Bonus$^\\text{T}$)")

## Produce latex table 
Table2 <- 
  TR("") %:% TR(seq(from = 1, to = 8, by = 1), se = TRUE, dec = 0) + 
  midrulep(list(c(2, 9))) +
  TR("") %:% TR(Label1_DepVar) %:% TR(Label1_DepVar) +
  TR("") %:% TR(Label2_DepVar) %:% TR(Label2_DepVar) +
  TR("") %:% TR(Label3_DepVar) %:% TR(Label3_DepVar) +
  midrule() +
  TR("\\textbf{Panel A: Baseline sample}") %:% TR(rep("", 8)) + 
  TR("Self-reported NearSubway $\\times$ Post")      %:% with(Table2_Plain, TR(beta[1:4], pvalues = cpval[1:4], dec = c(3, 3, 0, 3))) %:% TR(rep("", 4)) +
  TR("")                                             %:% with(Table2_Plain, TR(clse[1:4], se = TRUE,            dec = c(3, 3, 0, 3))) %:% TR(rep("", 4)) +
  TR("NearSubway $\\times$ Post") %:% TR(rep("", 4)) %:% with(Table2_Plain, TR(beta[5:8], pvalues = cpval[5:8], dec = c(3, 3, 0, 3))) +
  TR("")                          %:% TR(rep("", 4)) %:% with(Table2_Plain, TR(clse[5:8], se = TRUE,            dec = c(3, 3, 0, 3))) +
  vspace(10) +
  TR("Adj. R-Squared")                 %:% with(Table2_Plain, TR(adjR2[     1:8], dec = 2)) +
  TR("Obs")                            %:% with(Table2_Plain, TR(obs[       1:8], dec = 0)) +
  TR("N (affected group)")             %:% with(Table2_Plain, TR(N_affected[1:8], dec = 0)) +
  TR("N (control group)")              %:% with(Table2_Plain, TR(N_control[ 1:8], dec = 0)) +
  TR("Change on level variable")       %:% with(Table2_Plain, TR(ChangeL[   1:8], dec = 0)) +
  TR("Effect as \\% of SD of Dep Var") %:% with(Table2_Plain, TR(EffectOfSD[1:8], dec = 1, percentage = TRUE)) +
  midrule() +
  TR("\\textbf{Panel B: Propensity-score matched sample}") %:% TR(rep("", 8)) + 
  TR("Self-reported NearSubway $\\times$ Post")      %:% with(Table2_Plain, TR(beta[ 9:12], pvalues = cpval[9:12],   dec = c(3, 3, 0, 3))) %:% TR(rep("", 4)) +
  TR("")                                             %:% with(Table2_Plain, TR(clse[ 9:12], se = TRUE,               dec = c(3, 3, 0, 3))) %:% TR(rep("", 4)) +
  TR("NearSubway $\\times$ Post") %:% TR(rep("", 4)) %:% with(Table2_Plain, TR(beta[13:16], pvalues = cpval[13:16], dec = c(3, 3, 0, 3))) +
  TR("")                          %:% TR(rep("", 4)) %:% with(Table2_Plain, TR(clse[13:16], se = TRUE,              dec = c(3, 3, 0, 3))) +
  vspace(10) +
  TR("Adj. R-Squared")                 %:% with(Table2_Plain, TR(adjR2[     9:16], dec = 2)) +
  TR("Obs")                            %:% with(Table2_Plain, TR(obs[       9:16], dec = 0)) +
  TR("N (affected group)")             %:% with(Table2_Plain, TR(N_affected[9:16], dec = 0)) +
  TR("N (control group)")              %:% with(Table2_Plain, TR(N_control[ 9:16], dec = 0)) +
  TR("Change on level variable")       %:% with(Table2_Plain, TR(ChangeL[   9:16], dec = 0)) +
  TR("Effect as \\% of SD of Dep Var") %:% with(Table2_Plain, TR(EffectOfSD[9:16], dec = 1, percentage = TRUE))


## Output files (.tex and .pdf)
TS(Table2, file = "Table2",
     pretty_rules = TRUE, header = c('l',rep('c', 8)),
     output_path = paste0(getwd(), "/Output"))

## Open Table2.pdf with default PDF viewer
system2('open', args = 'Output/Table2.pdf', wait = FALSE)

## Remove intermediate products
rm(list = grep(pattern = "Main|fit|Label", ls(), value = TRUE))
